package com.liang.blog.system.service.impl;

import com.liang.blog.model.BlogRole;
import com.liang.blog.model.BlogRoleMenu;
import com.liang.blog.system.mapper.BlogMenuMapper;
import com.liang.blog.system.mapper.BlogRoleMapper;
import com.liang.blog.system.service.BlogRoleService;
import com.liang.blog.vo.role.RoleInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author：YanWenLiang
 * @Date:2023-09-08-16:09
 */
@Service
public class BlogRoleServiceImpl implements BlogRoleService {

    @Autowired
    private BlogRoleMapper blogRoleMapper;

    /**
     * 获取全部角色信息
     * @return
     */
    @Override
    public List<RoleInfoVo> getBlogRoleList(RoleInfoVo roleInfoVo) {
        return blogRoleMapper.getBlogRoleList(roleInfoVo);
    }

    /**
     * 获取全部角色
     * @return
     */
    @Override
    public List<RoleInfoVo> getBlogRoleAll() {
        return blogRoleMapper.getBlogRoleAll();
    }

    /**
     * 获取角色绑定的菜单组
     * @return
     */
    public List<Long> getRoleBingMenuIdById(Long id){
        return blogRoleMapper.getRoleBingMenuIdById(id);
    }

    @Override
    @Transactional
    public void updateRoleInfoByRid(RoleInfoVo roleInfoVo) {
        List<Long> addMenuIds = roleInfoVo.getAddMenuIds();
        List<Long> decreaseMenuIds = roleInfoVo.getDecreaseMenuIds();
        // 添加新增的菜单组
        if (!addMenuIds.isEmpty()){
            blogRoleMapper.addNewMenuIds(addMenuIds, roleInfoVo.getId());
        }
        // 删除菜单组
        if (!decreaseMenuIds.isEmpty()){
            blogRoleMapper.deleteMenuIds(decreaseMenuIds, roleInfoVo.getId());
        }

        blogRoleMapper.updateRoleById(roleInfoVo);
    }

    /**
     * 根据角色id删除角色信息
     * @param id
     * @return
     */
    @Override
    public int deleteRoleById(Long id) {
        return blogRoleMapper.deleteRoleById(id);
    }
}
